package com.smartcruise.report;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.smartcruise.R;
import com.smartcruise.base.DataEditContract;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by FengChaoQun
 * on 2018/1/23 0023
 * 语音adapter
 */

public class AudioAdapter extends ArrayAdapter<String> {

    private DataEditContract.Presenter mPresenter;
    private boolean modify;

    public AudioAdapter(@NonNull Context context, int resource, @NonNull List<String> objects, DataEditContract.Presenter presenter) {
        super(context, resource, objects);
        mPresenter = presenter;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.item_audio, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final NormalDialog normalDialog = new NormalDialog(getContext());
                normalDialog.content(getContext().getString(R.string.sureDeleteAudio));
                normalDialog.setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        normalDialog.dismiss();
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        mPresenter.reduceAudio(getItem(position));
                        normalDialog.dismiss();
                    }
                });
                normalDialog.show();
            }
        });

        viewHolder.audioImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JCVideoPlayerStandard.startFullscreen(getContext(), JCVideoPlayerStandard.class,
                        getItem(position), getContext().getString(R.string.audioRecord));
            }
        });

        if (modify) {
            viewHolder.cancel.setVisibility(View.VISIBLE);
        } else {
            viewHolder.cancel.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.audioImage)
        ImageView audioImage;
        @BindView(R.id.cancel)
        ImageView cancel;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public boolean isModify() {
        return modify;
    }

    public void setModify(boolean modify) {
        this.modify = modify;
    }
}
